package com.intel.service.Impl;

import com.intel.service.DownloadService;
import com.mchz.mdmp.webservice.MasterDataWebService;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 英磊联—洪焕青
 * @date 2018/11/7 15:49
 */
@Service
public class DownloadServiceImpl implements DownloadService{
    @Value("${wsdlUrl}")
    private String wsdlUrl;

    private static OMFactory fac = OMAbstractFactory.getOMFactory();
    private static SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    public void downloadStandardInfo(String token, String time){

        //拼装xml报文
        String xmlMessage = createXmlMessage(token, time);

        //调用接口
        String response = callSoapWs(xmlMessage);

        //根据接口的返回数据进行相应的处理

        System.out.println(response);
        //插入新建的数据库表，表结构要和ym_b_medicine一样
    }

    private String createXmlMessage(String token, String time) {
        OMElement upload = fac.createOMElement("UPLOAD", null);

        OMElement tokenOMElement = fac.createOMElement("TOKEN", null);
        tokenOMElement.setText(token);

        upload.addChild(tokenOMElement);

        OMElement medicineQueryContents = fac.createOMElement("MEDICINEQUERYCONTENTS", null);
//        OMElement queryContent = fac.createOMElement("QUERYCONTENT", null);
        OMElement column = fac.createOMElement("COLUMN", null);
        column.setText("INSERT_TIME");
        OMElement operator = fac.createOMElement("OPERATOR", null);
        operator.setText("GREATER_THAN");
        OMElement value = fac.createOMElement("VALUE", null);
        value.setText(time);

        medicineQueryContents.addChild(column);
        medicineQueryContents.addChild(operator);
        medicineQueryContents.addChild(value);

//        medicineQueryContents.addChild(queryContent);
        upload.addChild(medicineQueryContents);
        return upload.toString();
    }

    private String callSoapWs(String xmlParam) {
        String url = wsdlUrl;
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(MasterDataWebService.class);
        factory.setAddress(url);
        MasterDataWebService client = (MasterDataWebService)factory.create();
        return client.getMasterData(xmlParam);
    }

}
